<?php
namespace app\index\controller;
use app\index\model\Offer;
use app\index\model\OfferFunction;
use app\index\model\OfferMember;
use PHPExcel;
use PHPExcel_IOFactory;
use think\Controller;

class Index extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

    public function bjlist(){
        $offer = new Offer();
//        $data=$offer->where('isdelete',0)->order('updatetime', 'desc')->paginate(10);
        $data=$offer->where('isdelete',0)->order('updatetime', 'desc')->paginate(10);
        $this->assign("data",$data);
        return $this->fetch();
    }

    public function bjtable(){
        if($doid=input('get.id')){

            $offer = new Offer();
            $offermember = new OfferMember();
            $offerfunction = new OfferFunction();
            $data=$offer->where('isdelete',0)->order('updatetime', 'desc')->find();
            $offerfunctiondata['isdelete']='0';
            $offerfunctiondata['doid']=$doid;
            $functionlist=$offerfunction->where($offerfunctiondata)->select();
            $levelsum =$offerfunction->where($offerfunctiondata)->sum('doflevel');
            $data['dopsrice']=$data['doprice']*($data['doprofit']/100)+$data['doprice'];
            $avg =$data['dopsrice']/$levelsum;
            foreach ($functionlist as $k=>$v){
                $functionlist[$k]['price']=sprintf("%.2f", $functionlist[$k]['doflevel']*$avg);
            }
            $offermemberdata['isdelete']='0';
            $offermemberdata['doid']=$doid;
            $memberlist=$offermember->where($offermemberdata)->select();
            $this->assign("data",$data);
            $this->assign("memberlist",$memberlist);
            $this->assign("functionlist",$functionlist);
            return $this->fetch();
        }else{
            return "error";
        }
    }
    public function oncli(){
        $offer = new Offer();
        $data['dotitle']=input('post.dotitle');
        $data['docompany']=input('post.docompany');
        $data['doauthor']=input('post.doauthor');
        $data['doprofit']=input('post.doprofit');
        $data['doprice']=input('post.doprice');
        $data['docycle']=input('post.docycle');
        $data['createtime']=time();
        $data['updatetime']=time();
        $data['isdelete']=0;
        if($offer->save($data)){
            $doid=$offer->doid;
            for ($i=0;$i<count(input('post.domposition'));$i++){
                $offermember = new OfferMember();
                $offermemberdata['doid']=$doid;
                $offermemberdata['domposition']=input('post.domposition')[$i];
                $offermemberdata['dommumber']=input('post.dommumber')[$i];
                $offermemberdata['createtime']=time();
                $offermemberdata['updatetime']=time();
                $offermemberdata['isdelete']=0;
                if($offermember->save($offermemberdata)){
                    echo $offermember->domid;
                }

            }
            for ($i=0;$i<count(input('post.doftitle'));$i++){
                $offerfunction = new OfferFunction();
                $offerfunctiondata['doid']=$doid;
                $offerfunctiondata['doftitle']=input('post.doftitle')[$i];
                $offerfunctiondata['dofdescribe']=input('post.dofdescribe')[$i];
                $offerfunctiondata['doflevel']=input('post.doflevel')[$i];
                $offerfunctiondata['dofpid']=0;
                $offerfunctiondata['createtime']=time();
                $offerfunctiondata['updatetime']=time();
                $offerfunctiondata['isdelete']=0;
                if($offerfunction->save($offerfunctiondata)){
                    echo $offermember->domid;
                }

            }
            return "success";
        }else{
            return "error";
        }
    }

    public function datatoexcel($title,$data){
        $objPHPExcel = new \PHPExcel();
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
        $topNumber =2;
        $xlsTitle = iconv('utf-8', 'gb2312', $title);//文件名称
        // 设置表头信息
        $cellKey = array(
            'A','B','C','D','E','F','G','H','I','J','K','L','M',
            'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
            'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM',
            'AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ'
        );
        $cellName=array();
        $cellName[]=array('doid',"序号");
        $cellName[]=array('dotitle',"报价表标题");
        $cellName[]=array('docompany',"报价表所属公司");
        $cellName[]=array('doauthor',"报价表制作作者");
        $cellName[]=array('doprofit',"报价表所赚取的利润");
        $cellName[]=array('createtime',"创建时间");
        $cellName[]=array('updatetime',"修改时间");
        $objPHPExcel->getActiveSheet()->mergeCells('A1:'.$cellKey[count($cellName)-1].'1');//合并单元格（如果要拆分单元格是需要先合并再拆分的，否则程序会报错）
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1','订单信息');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
//        $objPHPExcel->setActiveSheetIndex(0)
//            ->setCellValue('A2', '序号')
//            ->setCellValue('B2', '报价表标题')
//            ->setCellValue('C2', '报价表所属公司')
//            ->setCellValue('D2', '报价表制作作者')
//            ->setCellValue('E2', '报价表所赚取的利润')
//            ->setCellValue('F2', '创建时间')
//            ->setCellValue('G2', '修改时间');
        foreach ($cellName as $k=>$v)
        {
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellKey[$k].$topNumber, $v[1]);//设置表头数据
            $objPHPExcel->getActiveSheet()->freezePane($cellKey[$k].($topNumber+1));//冻结窗口
            $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getFont()->setBold(true);//设置是否加粗
            $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中

        }
        /*--------------开始从数据库提取信息插入Excel表中------------------*/
//        for ($i=3;$i<=count($data)+2;$i++) {
//            $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $data[$i-$topNumber-1]['doid']);
//            $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $data[$i-$topNumber-1]['dotitle']);
//            $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $data[$i-$topNumber-1]['docompany']);
//            $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $data[$i-$topNumber-1]['doauthor']);
//            $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $data[$i-$topNumber-1]['doprofit']."%");
//            $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, date('Y-m-d H:i',$data[$i-$topNumber-1]['createtime']));
//            $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, date('Y-m-d H:i',$data[$i-$topNumber-1]['updatetime']));
//        }
//        dump($data);exit;
        foreach ($data as $k=>$v)
        {
            foreach ($cellName as $k1=>$v1)
            {

                $objPHPExcel->getActiveSheet()->setCellValue($cellKey[$k1].($k+1+$topNumber), $v[$v1[0]]);

            }
        }

        /*--------------下面是设置其他信息------------------*/

        $objPHPExcel->getActiveSheet()->setTitle('productaccess');      //设置sheet的名称
        $objPHPExcel->setActiveSheetIndex(0);                   //设置sheet的起始位置
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');   //通过PHPExcel_IOFactory的写函数将上面数据写出来

        $PHPWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel,"Excel2007");

        header('Content-Disposition: attachment;filename="'.$title.'.xlsx"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

        $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件

        exit;
    }

    public function fxoffer(){
        if($doid=input('get.id')){

            $offer = new Offer();
            $offermember = new OfferMember();
            $offerfunction = new OfferFunction();
            $data=$offer->where('isdelete',0)->order('updatetime', 'desc')->find();
            $offerfunctiondata['isdelete']='0';
            $offerfunctiondata['doid']=$doid;
            $functionlist=$offerfunction->where($offerfunctiondata)->select();
            $levelsum =$offerfunction->where($offerfunctiondata)->sum('doflevel');
            $data['dopsrice']=$data['doprice']*($data['doprofit']/100)+$data['doprice'];
            $avg =$data['dopsrice']/$levelsum;
            foreach ($functionlist as $k=>$v){
                $functionlist[$k]['price']=sprintf("%.2f", $functionlist[$k]['doflevel']*$avg);
            }
            $offermemberdata['isdelete']='0';
            $offermemberdata['doid']=$doid;
            $memberlist=$offermember->where($offermemberdata)->select();
            $this->assign("data",$data);
            $this->assign("memberlist",$memberlist);
            $this->assign("functionlist",$functionlist);
            return $this->fetch();
        }else{
            return "error";
        }
    }

}
